<?php
/**
 * 商品销量统计
 *
 * @author jc
 *
 */
namespace app\statis\command;

use app\common\model\Product;
use app\statis\lib\TimeStatistic;
use app\statis\model\StatProductModel;
use app\statis\model\StatProductSalesModel;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;
use think\facade\App;
use think\facade\Log;

class ProductSales extends Command
{

    protected function configure()
    {
        $this->setName('product_sales')
            ->setDescription('商品销量统计');
    }

    protected function execute(Input $input, Output $output)
    {
        Log::init([
            'path' => App::getRuntimePath() . 'log/stat/product_sales/',
        ]);

        $output->info('----product_sales start_time ----' . date('Y-m-d H:i:s', time()));

        /*业务处理*/
        $statProductSalesM  = new StatProductSalesModel();
        $statProductM       = new StatProductModel();
        /*获取统计时间段*/
        $stat_time          = '2019-09-10';
        $stat_start_time    = TimeStatistic::getStartTime($stat_time);
        $stat_end_time      = TimeStatistic::getEndTime($stat_time);

        Db::startTrans();
        try {
            /*统计自提商品销量*/
            $statProductSalesM->statistic($stat_start_time, $stat_end_time, Product::DISPATCHING_TYPE_SELF_PICK);
            /*统计物流商品销量*/
            $statProductSalesM->statistic($stat_start_time, $stat_end_time, Product::DISPATCHING_TYPE_DELIVER);
            /*商品汇总数据统计*/
            $statProductM->summary($stat_start_time, $stat_end_time);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $output->info('error:' . $e->getMessage());
        }
        $output->info('----product_sales end');
    }

}